import { _decorator, AudioClip, AudioSource, Component, Node, Prefab, tween, UITransform, Vec3 } from 'cc';
import { layerAction_001 } from './layerAction-001';
import { layerAction_003 } from './layerAction-003';
import { layerAction_002 } from './layerAction-002';
import { gameState } from './gameState';
import { preBlockClick } from './preBlockClick';
import { mainClick } from './mainClick';
import { eventDispatcher } from './eventDispatcher';
const { ccclass, property } = _decorator;

@ccclass('layerRootAction')
export class layerRootAction extends Component {
    layer1: layerAction_001 = null;
    @property(layerAction_002) layer2: layerAction_002 = null;
    @property(layerAction_003) layer3: layerAction_003 = null;
    audio_source: AudioSource = null;
    @property([AudioClip]) audio_clip_array: AudioClip[] = [];
    /**加载layer1 */
    private load_layer1_action(){
        if(!this.layer1){
            this.layer1 = this.node.getChildByName("layer1").getComponent(layerAction_001);

        }
    }
    start_game() {
        this.load_layer1_action();
        this.layer1.clear_all();
        this.layer2.clear_all();
        this.layer3.clear_all();
        this.layer1.start_game(gameState.cur_lvl);
        this.layer1.random_block();
    }
    click_redo() {
        if (gameState.ad_redo_times <= 0) {
            eventDispatcher.get_target().emit(eventDispatcher.TIPS_MSG, "redo times is 0");
            return;
        }
        let redo_block = this.layer3.get_redo_block();
        if (!redo_block) {
            return;
        }
        {
            gameState.ad_redo_times-=1;
            eventDispatcher.get_target().emit(eventDispatcher.REFRESH_BUTTONS);
        }
        //find block for time
        
        redo_block.node.setParent(this.node);
        redo_block.node.setPosition(this.node.getComponent(UITransform).convertToNodeSpaceAR(redo_block.get_temp_pos()));

        let target_pos = this.node.getComponent(UITransform).convertToNodeSpaceAR(redo_block.original.node.getWorldPosition());

        tween(redo_block.node)
            .to(0.15, { position: target_pos })
            .call(() => {
                redo_block.original.node.active = true;
                this.layer1.refresh_shadow();
            }).removeSelf().start();
        this.layer3.reset_order();
    }
    click_random() {
        {
            if (gameState.ad_random_times <= 0) {
                eventDispatcher.get_target().emit(eventDispatcher.TIPS_MSG, "random times is 0");
                return;
            } else {
                gameState.ad_random_times -= 1;
                eventDispatcher.get_target().emit(eventDispatcher.REFRESH_BUTTONS);
            }
        }
        this.layer1.random_block();
    }
    click_remove() {
        if (this.layer3.get_block_size() == 0) return;

        {
            if (gameState.ad_remove_times <= 0) {
                eventDispatcher.get_target().emit(eventDispatcher.TIPS_MSG, "remove times is 0");
                return;
            } else {
                gameState.ad_remove_times -= 1;
                eventDispatcher.get_target().emit(eventDispatcher.REFRESH_BUTTONS);
            }
        }
        
        this.remove();
    }
    private remove() {
        let temp = this.layer3.get_remove_block();
        if (temp.length == 0) {
            return;
        }
        this.layer3.reset_order();
        let local = this.node.getComponent(UITransform).convertToNodeSpaceAR(this.layer2.get_zero_world_position());
        for (let ele of temp) {
            ele.node.setParent(this.node);
            ele.node.setPosition(this.node.getComponent(UITransform).convertToNodeSpaceAR(ele.get_temp_pos()))

            tween(ele.node)
                .to(0.1, { position: local })
                .call(() => {
                    this.layer2.add_block(ele);
                })
                .start();
        }
    }
    to_3_from_1(block: preBlockClick, pre_block: Prefab) {
        let clone_block = block.clone_block(this.node, pre_block);
        this.layer1.refresh_shadow();

        let slot_pos = this.layer3.get_slot_position(clone_block);
        let local_pos = this.node.getComponent(UITransform).convertToNodeSpaceAR(slot_pos);

        tween(clone_block.node)
            .to(0.15, { position: local_pos })
            .call(() => {
                this.layer3.add(clone_block);
                let del: boolean = this.layer3.del_same_block();
                if (del) {
                    this.play_sound(1);
                    if (this.layer1.get_block_size() + this.layer2.get_block_size() + this.layer3.get_block_size() === 0) {
                        //下一关
                        this.node.parent.getComponent(mainClick).next_level();
                    }
                }
                this.check_over();
            }).start();
    }
    to_3_from_2(block: preBlockClick) {

        block.node.setParent(this.node);
        block.node.setPosition(this.node.getComponent(UITransform).convertToNodeSpaceAR(block.get_temp_pos()));

        let slot_pos = this.layer3.get_slot_position(block);
        let local_pos = this.node.getComponent(UITransform).convertToNodeSpaceAR(slot_pos);

        tween(block.node)
            .to(0.15, { position: local_pos })
            .call(() => {
                this.layer3.add(block);
                let del: boolean = this.layer3.del_same_block();
                if (del) {
                    this.play_sound(1);
                    if (this.layer1.get_block_size() + this.layer2.get_block_size() + this.layer3.get_block_size() === 0) {
                        //下一关
                        this.node.parent.getComponent(mainClick).next_level();
                    }
                }
                this.check_over();
            }).start();
    }
    check_over() {
        if (this.get_layer_3_size() >= 7) {
            eventDispatcher.get_target().emit(eventDispatcher.OPEN_REVIVE);
        }
    }
    get_layer_3_size() {
        return this.layer3.get_block_size();
    }
    play_sound(index: number): void {
        this.audio_source.playOneShot(this.audio_clip_array[index]);
    }
    start() {
        this.load_layer1_action();
        this.audio_source = this.node.getComponent(AudioSource);
        eventDispatcher.get_target().on(eventDispatcher.REMOVE_ACTION, this.remove, this);
    }

    update(deltaTime: number) {

    }
}


